<?php

  if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );

  // -- webMS Modification --
  // Check if osCommerce environment is already loaded
  if (defined( '_OSCOMMERCE_LOADED' )) {
    if (!DB_SHARED) {
      // Reconnect to osCommerce Database
      tep_db_connect() or die('Unable to connect to database server!');
    }
    // Load osCommerce Globals for Boxes
    global $category_links, $languages_id, $catID, $catID_array, $current_category_id;
    global $request_type, $lng, $cart, $currencies, $language, $navigation, $breadcrumb;
  } else {
    // Load osCommerce Environment for the First Time
    require('components/'.$option.'/includes/application_top.php');
  }

  require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_WHOLESALE);

  $error = false;
  if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'send')) {
    $name = tep_db_prepare_input($_REQUEST['name']);
    $email_address = tep_db_prepare_input($_REQUEST['email']);
    $email = $email_address;
    $enquiry = tep_db_prepare_input($_REQUEST['enquiry']);
    $store_name = tep_db_prepare_input($_REQUEST['store_name']);
    $store_phone = tep_db_prepare_input($_REQUEST['store_phone']);
    $store_address = tep_db_prepare_input($_REQUEST['store_address']);
    $store_tax_id = tep_db_prepare_input($_REQUEST['store_tax_id']);

    if (tep_validate_email($email_address)) {
      tep_mail(STORE_OWNER, WHOLESALE_EMAIL, EMAIL_SUBJECT, 'Store Name:  ' . $store_name . "\r\n\r\n" . 'Name:  ' . $name . "\r\n\r\n" . 'Phone:  ' . $store_phone . "\r\n\r\n" . 'Address:  ' . $store_address . "\r\n\r\n" . 'Tax ID: ' . $store_tax_id . "\r\n\r\n" . 'Comments / Special Instructions:  ' . $enquiry, $name, $email_address);

      tep_redirect(tep_href_link(FILENAME_WHOLESALE, 'action=success'));
    } else {
      $error = true;

      $messageStack->add('contact', ENTRY_EMAIL_ADDRESS_CHECK_ERROR);
    }
  }

  $breadcrumb->add(NAVBAR_TITLE, tep_href_link(FILENAME_WHOLESALE));
 require(DIR_WS_INCLUDES . 'header.php'); 
 $a['form'] = tep_draw_form('wholesale_request', tep_href_link(FILENAME_WHOLESALE, 'action=send'));
 $a['heading'] = HEADING_TITLE;
 
  if ($messageStack->size('contact') > 0) {
  $a['message'] = $messageStack->output('contact');

  }

  if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'success')) {
  $a['action'] = 'success';
  $a['image'] = tep_image(DIR_WS_IMAGES . 'table_background_man_on_board.gif', HEADING_TITLE, '0', '0', 'align="left"');

$a['pagetext'] = TEXT_SUCCESS;
$a['button_continue'] = '<a href="' . tep_href_link(FILENAME_DEFAULT) . '">' . tep_image_button('button_continue.gif', IMAGE_BUTTON_CONTINUE) . '</a>';
  } else {
//form variables

$a['pagetext'] = ENTRY_WHOLESALE_TEXT;
$a['button_continue'] = tep_image_submit('button_continue.gif', IMAGE_BUTTON_CONTINUE);
  }

require(WS_TEMPLATES . E_TEMPLATE_FOLDER . '/'.$osMod.'.html');
require(DIR_WS_INCLUDES . 'footer.php');
require(DIR_WS_INCLUDES . 'application_bottom.php');

  // -- webMS Modification --
  // reset the Mambo Database connection (stomped by osCommerce Connection)
  if (!DB_SHARED) {
    global $database, $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix;
    $database = new database( $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix );
  }
 ?>